#include "../heads/print_chess_board.h"
#include "../heads/Const.h"

void printNestedSquares(std::vector<std::vector<Node>> nestedNodes) {
    int size = 4 * BORDER_SIZE; // 最大正方形的边长
    std::vector<std::vector<int>> canvas(size+4, std::vector<int>(size+4, 0)); // 创建一个二维数组画布

    // 定义边界函数
    auto set_border = [&](int offset, int border_size) {
        for (int i = offset; i < offset + border_size; ++i) {
            canvas[offset][i] = int(State::H_LINE); // 上边界
            canvas[offset + border_size - 1][i] = int(State::H_LINE); // 下边界
            canvas[i][offset] = int(State::V_LINE); // 左边界
            canvas[i][offset + border_size - 1] = int(State::V_LINE); // 右边界
        }
    };

    // 设置外层正方形的边界
    set_border(0, size);
    // 设置中间正方形的边界
    set_border(BORDER_SIZE, size - 2 * BORDER_SIZE);
    // 设置内层正方形的边界
    set_border(2 * BORDER_SIZE - BORDER_SIZE / 2, BORDER_SIZE);

     // 绘制连接线的函数
    auto draw_line = [&](int x1, int y1, int x2, int y2,State S) {
        int dx = (x2 - x1) > 0 ? 1 : -1;
        int dy = (y2 - y1) > 0 ? 1 : -1;
        int x = x1;
        int y = y1;
        
        while (x != x2 && y != y2) {
          
        canvas[x][y] = int(S);
          
            x += dx;
            y += dy;
        }
    };

    // 绘制四条连接线
    // 左上角
    draw_line(0, 0, BORDER_SIZE, BORDER_SIZE,State::C_LINE_2);
    draw_line(BORDER_SIZE, BORDER_SIZE, 2 * BORDER_SIZE - BORDER_SIZE / 2, 2 * BORDER_SIZE - BORDER_SIZE / 2,State::C_LINE_2);
    // 左下角
    draw_line(size - 1, 0, 3 * BORDER_SIZE - 1, BORDER_SIZE,State::C_LINE_1);
    draw_line(3 * BORDER_SIZE - 1, BORDER_SIZE, 2 * BORDER_SIZE + BORDER_SIZE / 2-1, 2 * BORDER_SIZE - BORDER_SIZE / 2,State::C_LINE_1);
    // 右上角
    draw_line(0, size - 1, BORDER_SIZE, 3 * BORDER_SIZE - 1,State::C_LINE_1);
    draw_line(BORDER_SIZE, 3 * BORDER_SIZE - 1, 2 * BORDER_SIZE - BORDER_SIZE / 2, 2 * BORDER_SIZE + BORDER_SIZE / 2 - 1,State::C_LINE_1);
    // 右下角
    draw_line(size - 1, size - 1, 3 * BORDER_SIZE - 1, 3 * BORDER_SIZE - 1,State::C_LINE_2);
    draw_line(3 * BORDER_SIZE - 1, 3 * BORDER_SIZE - 1, 2 * BORDER_SIZE + BORDER_SIZE / 2 - 1, 2 * BORDER_SIZE + BORDER_SIZE / 2 - 1,State::C_LINE_2);
    
        // 连接水平线
    auto draw_horizontal_line = [&](int y, int x_start, int x_end) {
        for (int x = x_start; x <= x_end; ++x) {
            canvas[y][x] = int(State::H_LINE);
        }
    };

    // 连接垂直线
    auto draw_vertical_line = [&](int x, int y_start, int y_end) {
        for (int y = y_start; y <= y_end; ++y) {
            canvas[y][x] = int(State::V_LINE);
        }
    };
    
        // 水平中点连接线
        draw_vertical_line(2*BORDER_SIZE, 0, BORDER_SIZE+BORDER_SIZE/2); // 连接内层和中层正方形的上边中点
        draw_vertical_line(2*BORDER_SIZE, 2*BORDER_SIZE+BORDER_SIZE/2, 4*BORDER_SIZE-1); // 连接内层和中层正方形的上边中点
        draw_horizontal_line(2*BORDER_SIZE, 0, BORDER_SIZE+BORDER_SIZE/2); // 连接内层和中层正方形的下边中点
        draw_horizontal_line(2*BORDER_SIZE, 2*BORDER_SIZE+BORDER_SIZE/2, 4*BORDER_SIZE-1); // 连接内层和中层正方形的下边中点
    
    // 指定连接点位置
    std::vector<std::vector<std::pair<int, int>>>  groups_of_points  = {
        {
        {0, 0},
        {0, 2*BORDER_SIZE},
        {0, 4*BORDER_SIZE-1},
        },
         {
        {BORDER_SIZE, BORDER_SIZE},
        {BORDER_SIZE, 2*BORDER_SIZE},
        {BORDER_SIZE, 3*BORDER_SIZE-1},
         },
         {
        {BORDER_SIZE+BORDER_SIZE/2+1, BORDER_SIZE+BORDER_SIZE/2+1},
        {BORDER_SIZE+BORDER_SIZE/2+1, 2*BORDER_SIZE},
        {BORDER_SIZE+BORDER_SIZE/2+1, 2*BORDER_SIZE+BORDER_SIZE/2},
         },
         {
        {2*BORDER_SIZE, 0},
        {2*BORDER_SIZE, BORDER_SIZE},
        {2*BORDER_SIZE, BORDER_SIZE+BORDER_SIZE/2+1},
        {2*BORDER_SIZE, 2*BORDER_SIZE+BORDER_SIZE/2},
        {2*BORDER_SIZE, 3*BORDER_SIZE-1},
        {2*BORDER_SIZE, 4*BORDER_SIZE-1},
         },
          {
        {2*BORDER_SIZE+BORDER_SIZE/2, BORDER_SIZE+BORDER_SIZE/2+1},
        {2*BORDER_SIZE+BORDER_SIZE/2, 2*BORDER_SIZE},
        {2*BORDER_SIZE+BORDER_SIZE/2, 2*BORDER_SIZE+BORDER_SIZE/2},
         },
          {
        {3*BORDER_SIZE-1, BORDER_SIZE},
        {3*BORDER_SIZE-1, 2*BORDER_SIZE},
        {3*BORDER_SIZE-1, 3*BORDER_SIZE-1},
         },
          {
        {4*BORDER_SIZE-1, 0},
        {4*BORDER_SIZE-1, 2*BORDER_SIZE},
        {4*BORDER_SIZE-1, 4*BORDER_SIZE-1},
        },
        // 可以继续添加更多的点
    };


    int index = 0;
    for (size_t i = 0; i < groups_of_points.size(); ++i) {
    // 现在遍历每一组内的点
    for (size_t j = 0; j < groups_of_points[i].size(); ++j) {
        // 使用索引来获取点的坐标
        int x = groups_of_points[i][j].first;  // 获取横坐标
        int y = groups_of_points[i][j].second; // 获取纵坐标

        //从棋盘图获得的该节点的位置
        State st = nestedNodes[i][j].get_state();
        canvas[x][y] = int(st);

        // 其他操作...
    }
    }
    
    canvas[2 * BORDER_SIZE][4 * BORDER_SIZE] = 11;
    // 根据画布数组输出图案
    for (int i = 0; i < canvas.size();i++)
    {
        for (int j = 0; j < canvas[i].size();j++) {
            
            if (i==0&&j==4 * BORDER_SIZE+1)
            {
                std::cout << ("0");
                continue;
            }
            else if(i==BORDER_SIZE&&j==4 * BORDER_SIZE+1)
            {
                std::cout << ("1");
                continue;
            }
          
             else if (i==2 *BORDER_SIZE&&j==4 * BORDER_SIZE+2)
            {
                std::cout << ("2");
                continue;
            }

               else if (i==3 *BORDER_SIZE-1&&j==4 * BORDER_SIZE+1)
            {
                std::cout << ("3");
                continue;
            }
             else if (i==4 *BORDER_SIZE-1&&j==4 * BORDER_SIZE+1)
            {
                std::cout << ("4");
                continue;
            }

            else if (i==4 * BORDER_SIZE +1&&j==0)
            {
                std::cout << (" 0 ");
                continue;
            }
            else if (i==4 * BORDER_SIZE +1&&j==BORDER_SIZE)
            {
                std::cout << (" 1 ");
                continue;
            }
            else if (i==4 * BORDER_SIZE +1&&j==2*BORDER_SIZE)
            {
                std::cout << (" 2 ");
                continue;
            }
             else if (i==4 * BORDER_SIZE +1&&j==3*BORDER_SIZE-1)
            {
                std::cout << (" 3 ");
                continue;
            }
             else if (i==4 * BORDER_SIZE +1&&j==4*BORDER_SIZE-1)
            {
                std::cout << (" 4 ");
                continue;
            }
            
            // if(i==1&&j==0)
            // {
            //     std::cout << ("A");
            //     continue;

            // }
            // else if (i==1&&j==BORDER_SIZE*2)
            // {
            //     std::cout << ("B");
            //     continue;
            // }
            // else if (i==1&&j==BORDER_SIZE*4-2)
            // {
            //     std::cout << ("C");
            //     continue;
            // }
            //  else if (i==BORDER_SIZE-1&&j==BORDER_SIZE-1)
            // {
            //     std::cout << ("D");
            //     continue;
            // }
            //  else if (i==BORDER_SIZE-1&&j==BORDER_SIZE*2)
            // {
            //     std::cout << ("E");
            //     continue;
            // }
            // else if (i==BORDER_SIZE-1&&j==BORDER_SIZE*3)
            // {
            //     std::cout << ("F");
            //     continue;
            // }
            // else if (i==2*BORDER_SIZE-BORDER_SIZE/2-1&&j==BORDER_SIZE+BORDER_SIZE/2)
            // {
            //     std::cout << ("G");
            //     continue;
            // }
            //  else if (i==2*BORDER_SIZE-BORDER_SIZE/2-1&&j==BORDER_SIZE*2)
            // {
            //     std::cout << ("H");
            //     continue;
            // }
            //   else if (i==2*BORDER_SIZE-BORDER_SIZE/2-1&&j==BORDER_SIZE*2+BORDER_SIZE/2)
            // {
            //     std::cout << ("I");
            //     continue;
            // }
            //   else if (i==2*BORDER_SIZE-1&&j==0)
            // {
            //     std::cout << ("J");
            //     continue;
            // }
            //  else if (i==2*BORDER_SIZE-1&&j==BORDER_SIZE)
            // {
            //     std::cout << ("K");
            //     continue;
            // }
            // else if (i==2*BORDER_SIZE&&j==2*BORDER_SIZE-BORDER_SIZE/2+1)
            // {
            //     std::cout << ("L");
            //     continue;
            // }
            // else if (i==2*BORDER_SIZE&&j==2*BORDER_SIZE+BORDER_SIZE/2-1)
            // {
            //     std::cout << ("M");
            //     continue;
            // }
            // else if (i==3*BORDER_SIZE-BORDER_SIZE-1&&j==3*BORDER_SIZE-1)
            // {
            //     std::cout << ("N");
            //     continue;
            // }
            // else if (i==2*BORDER_SIZE-1&&j==4*BORDER_SIZE-1)
            // {
            //     std::cout << ("O");
            //     continue;
            // }
            // else if (i==3*BORDER_SIZE-BORDER_SIZE/2-1&&j==BORDER_SIZE+BORDER_SIZE/2)
            // {
            //     std::cout << ("P");
            //     continue;
            // }

            // else if (i==2*BORDER_SIZE+BORDER_SIZE/2-1&&j==BORDER_SIZE+BORDER_SIZE)
            // {
            //     std::cout << ("Q");
            //     continue;
            // }

            // else if (i==3*BORDER_SIZE-BORDER_SIZE/2-1&&j==3*BORDER_SIZE-BORDER_SIZE/2)
            // {
            //     std::cout << ("R");
            //     continue;
            // }
            // else if (i==3*BORDER_SIZE&&j==BORDER_SIZE)
            // {
            //     std::cout << ("S");
            //     continue;
            // }
            // else if (i==3*BORDER_SIZE&&j==BORDER_SIZE)
            // {
            //     std::cout << ("S");
            //     continue;
            // }


            if(canvas[i][j]==int(State::EMPTY))
            {
                std::cout << ("   ");
            }
             else if (canvas[i][j]==int(State::C_LINE_1))
           {
           
             std::cout << " ╱ ";
           }
             else if (canvas[i][j]==int(State::C_LINE_2))
           {
           
             std::cout << " ╲ ";
           }
             else if (canvas[i][j]==int(State::V_LINE))
           {
           
             std::cout << " │ ";
           }
             else if (canvas[i][j]==int(State::H_LINE))
           {
           
             std::cout << "───";
           }
           else if (canvas[i][j]==int(State::PLAYER1))
           {
             std::cout << (" ● ");
           }
            else if (canvas[i][j]==int(State::PLAYER2))
           {
           
             std::cout << " █ ";
           }
          
           else if (canvas[i][j]==int(State::HAO))
           {
           
             std::cout << " ✖ ";
           }
           
        }
        std::cout << std::endl;
    }
}